//
//  WSTableViewManager.m
//  Component
//
//  Created by 王诗文 on 2018/7/26.
//  Copyright © 2018年 王诗文. All rights reserved.
//

#import "WSTableViewManager.h"
#import "UITableViewHeaderFooterView+WSExtension.h"
#import "UITableView+WSExtension.h"
#import "WSListPlaceHolderView.h"
#import <UIKit/UIKit.h>
#import "WSModule.h"
#import "WSTableCellProtocol.h"


@interface WSTableViewManager()<WSTableViewHeaderFooterActionDelegate,WSTableCellSubViewsActionDelegate,WSListPlaceHolderViewDelegate>
@property(assign, nonatomic) BOOL isDragging;
@end

@implementation WSTableViewManager


- (BOOL)wsShowHolder{
     if (self.modules.count==0&&(self.listStatus==WSListStatusNone||self.listStatus==WSListStatusFails||self.listStatus==WSListStatusLogin)) {
          return true;
     }
     return false;
}

#pragma mark - delegate

//个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     if ([self wsShowHolder]) {
          self.holderView.hidden = false;
          self.tableView.hidden = true;
          return 0;
     }
     self.holderView.hidden = true;
     self.tableView.hidden = false;
     self.isDragging = NO;
     return self.modules.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    return module.datas.count;
}

//高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WSModule *module = self.modules[indexPath.section];
    Class <WSTableCellProtocol>cls = NSClassFromString(module.cellName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module.datas[indexPath.row]].height;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
     if (module.headerName==nil) return [self tableViewLineDefaultheight];
    Class <WSTableCellProtocol>cls = NSClassFromString(module.headerName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module].height;
    }
     return [self tableViewLineDefaultheight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    if (module.footerName==nil) return [self tableViewLineDefaultheight];
    Class <WSTableCellProtocol>cls = NSClassFromString(module.footerName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module].height;
    }
    return [self tableViewLineDefaultheight];
}

//view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    if (module.headerName==nil) return nil;
    UITableViewHeaderFooterView <WSTableCellProtocol>*header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:module.headerName];
    if ([header respondsToSelector:@selector(wsFillContent:)]) {
        [header wsFillContent:module.headerData];
    }
    header.wsDelegate = self;
    header.section = section;
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    if (module.footerName==nil) return nil;
    UITableViewHeaderFooterView <WSTableCellProtocol>*footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:module.footerName];
    if ([footer respondsToSelector:@selector(wsFillContent:)]) {
        [footer wsFillContent:module.footerData];
    }
    footer.wsDelegate = self;
    footer.section = section;
    return footer;
}

//cell
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WSModule *module = self.modules[indexPath.section];
    NSAssert(module.cellName!=nil, @"cellname must not nil");
     UITableViewCell <WSTableCellProtocol>*cell = [tableView dequeueReusableCellWithIdentifier:module.cellName];
     cell.attachObj = self.attachObj;
     cell.wsDelegate = self;
     cell.indexPath = indexPath;
    if ([cell respondsToSelector:@selector(wsFillContent:)]) {
        [cell wsFillContent:module.datas[indexPath.row]];
    }
    return cell;
}


- (CGFloat)tableViewLineDefaultheight{
     if (self.tableView.style == UITableViewStyleGrouped) {
          return 0.1;
     }
     return 0;
}

//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.cellClickedBlock) {
        WSModule *module = self.modules[indexPath.section];
        self.cellClickedBlock(indexPath,module.datas[indexPath.row]);
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
     self.isDragging = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     if (self.scrollViewDidScrollBlock && self.isDragging) {
          self.scrollViewDidScrollBlock(scrollView);
     }
}
//cell 内部点击事件
- (void)wsCellSubViewsClickedIndexPath:(NSIndexPath *)indexPath data:(id)data{
     if (self.cellsubViewsClickedBlock) {
          self.cellsubViewsClickedBlock(indexPath, data);
     }
}

- (void)cellClickedInSection:(NSIndexPath *)indexPath data:(id)data{
     if (self.cellClickedBlock) {
          self.cellClickedBlock(indexPath, data);
     }
}

//header footer 点击事件代理方法
- (void)headerClickedInSection:(NSInteger)section data:(id)data{
    if (self.headerClickedBlock) {
        self.headerClickedBlock(section,data);
    }
}

- (void)footerClickedInSection:(NSInteger)section data:(id)data{
    if (self.footerClickedBlock) {
        self.footerClickedBlock(section,data);
    }
}

- (void)holderButtonClickWithStatus:(WSListStatus)status
{
     if (self.placeHolderBlock) {
          self.placeHolderBlock(status);
     }
}

- (void)registerCells{
    if (_modules&&_tableView) {
        [_modules enumerateObjectsUsingBlock:^(WSModule * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tableView wsRegisterCellName:obj.cellName];
            [self.tableView wsRegisterHeaderFooterName:obj.headerName];
            [self.tableView wsRegisterHeaderFooterName:obj.footerName];
        }];
    }
}

#pragma mark - set\get

- (void)setModules:(NSArray<WSModule *> *)modules{
    _modules = modules;
    [self registerCells];
}

- (void)setTableView:(UITableView *)tableView{
    _tableView = tableView;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self registerCells];
}

- (void)setListStatus:(WSListStatus)listStatus{
     _listStatus = listStatus;
     self.holderView.listStatus = listStatus;
}

- (WSListPlaceHolderView *)holderView{
     if (_holderView==nil) {
          _holderView = [[WSListPlaceHolderView alloc] initWithFrame:self.tableView.frame];
          _holderView.delegate = self;
          _holderView.hidden = true;
          [self.tableView.superview addSubview:_holderView];
          [self.tableView.superview sendSubviewToBack:_holderView];
     }
     return _holderView;
}
@end
